/** 
 * 文 件 名:SportManageLogic.java
 * Copyright (C) 2007-2012:杭州希禾信息技术有限公司 版权所有 
 * 创 建 人:刘绍辉
 * 日    期:2012-3-12 下午3:36:17
 * 修 改 人: 
 * 日   期:
 * 修改描述:
 * 描   述:TODO
 */
package net.seehealth.healthpad.business.foodmanage;

import java.util.ArrayList;
import java.util.List;

import net.seehealth.healthpad.application.base.BaseLogic;
import net.seehealth.healthpad.application.base.BaseModel;
import net.seehealth.healthpad.business.foodmanage.service.IFoodManageLogic;
import net.seehealth.healthpad.dataaccess.HealthSqliteHelper;
import net.seehealth.healthpad.model.foodmanage.Food;
import net.seehealth.healthpad.model.foodmanage.FoodGuide;
import net.seehealth.healthpad.model.foodmanage.FoodType;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @className SportManageLogic
 * @author 刘绍辉
 * @description TODO 食材业务类
 * @date 2012-3-12 下午5:22:54
 */
public class FoodManageLogic extends BaseLogic implements IFoodManageLogic {
	private HealthSqliteHelper helper = null;
	private final static String TAG = "FoodManageLogic";
	public FoodManageLogic(Context context){
		helper = new HealthSqliteHelper(context);
	}
	@Override
	public FoodGuide getLocalFoodGuideData(int userId) {
		FoodGuide foodGuide = new FoodGuide();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor foodGuides = db.query("foodguide", null, "userID = ?", new String []{userId+""}, null, null, null);
		if(foodGuides.moveToFirst()){
			foodGuide.setFid(foodGuides.getInt(foodGuides.getColumnIndex("fid")));
			foodGuide.setUserID(foodGuides.getInt(foodGuides.getColumnIndex("userID")));
			foodGuide.setNowWeight(foodGuides.getFloat(foodGuides.getColumnIndex("nowWeight")));
			foodGuide.setIdeaWeight(foodGuides.getFloat(foodGuides.getColumnIndex("ideaWeight")));
			foodGuide.setIdeaWeight(foodGuides.getFloat(foodGuides.getColumnIndex("ideaWeight")));
			foodGuide.setStandardIntake(foodGuides.getFloat(foodGuides.getColumnIndex("standardIntake")));
			foodGuide.setTodayIntake(foodGuides.getFloat(foodGuides.getColumnIndex("todayIntake")));
			foodGuide.setFoodName(foodGuides.getString(foodGuides.getColumnIndex("foodName")));
			foodGuide.setPractice(foodGuides.getString(foodGuides.getColumnIndex("practice")));
			foodGuide.setPng(foodGuides.getString(foodGuides.getColumnIndex("png")));
			foodGuide.setFoodAdvice(foodGuides.getString(foodGuides.getColumnIndex("foodAdvice")));
			foodGuide.setFoodTaboo(foodGuides.getString(foodGuides.getColumnIndex("foodTaboo")));
		}
		foodGuides.close();
		db.close();
		return foodGuide;
	}
	/**
	 *  获取所有食材
	  * @description:TODO
	  * @param 
	  * @return 
	  * @throws Exception   
	  * @author TODO
	  * @date 2012-4-12 上午11:46:18
	 */
	public List<Food> getLocalFoods(){
		List<Food> foods = new ArrayList<Food>();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.query("food", null, null, null, null, null, null);
		for (cursor.moveToFirst();!(cursor.isAfterLast());cursor.moveToNext()) {
			Food food = new Food();
			food.setNid(cursor.getInt(cursor.getColumnIndex("fid")));
			food.setKc(cursor.getFloat(cursor.getColumnIndex("kc")));
			food.setDesc(cursor.getString(cursor.getColumnIndex("desc")));
			food.setTypeCode(cursor.getString(cursor.getColumnIndex("typeCode")));
			food.setName(cursor.getString(cursor.getColumnIndex("name")));
			food.setCode(cursor.getString(cursor.getColumnIndex("code")));
			foods.add(food);
		}
		cursor.close();
		db.close();
		return foods;
	}
	/**
	 *  获取所有食材类型
	  * @description:TODO
	  * @param 
	  * @return 
	  * @throws Exception   
	  * @author TODO
	  * @date 2012-4-12 上午11:48:30
	 */
	public List<FoodType> getLoaclFoodTypes(){
		List<FoodType> foodTypes = new ArrayList<FoodType>();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.query("foodtype", null, null, null, null, null, null);
		for (cursor.moveToFirst();!(cursor.isAfterLast());cursor.moveToNext()) {
			FoodType foodType = new FoodType();
			foodType.setFtid(cursor.getInt(cursor.getColumnIndex("ftid")));
			foodType.setFoodTypeCode(cursor.getString(cursor.getColumnIndex("foodTypeCode")));
			foodType.setFoodTypeName(cursor.getString(cursor.getColumnIndex("foodTypeName")));
			foodType.setParentID(cursor.getInt(cursor.getColumnIndex("parentID")));
			foodTypes.add(foodType);
		}
		cursor.close();
		db.close();
		return foodTypes;
	}
	@Override
	public boolean saveLocalFoodGuide(FoodGuide foodGuide) {
		boolean bool = false;
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues  values = new ContentValues();
		values.put("userID", foodGuide.getUserID());
		values.put("nowWeight", foodGuide.getNowWeight());
		values.put("ideaWeight", foodGuide.getIdeaWeight());
		values.put("standardIntake", foodGuide.getStandardIntake());
		values.put("todayIntake", foodGuide.getTodayIntake());
		values.put("foodName", foodGuide.getFoodName());
		values.put("practice", foodGuide.getPractice());
		values.put("png", foodGuide.getPng());
		values.put("foodAdvice", foodGuide.getFoodAdvice());
		values.put("foodTaboo", foodGuide.getFoodTaboo());
		long num = db.insert("foodguide", null, values);
		if(num!=0){
			bool = true;
		}
		return bool;
	}
	@Override
	public boolean saveLocalFood(Food food) {
		boolean bool = false;
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues  values = new ContentValues();
		values.put("code", food.getCode());
		values.put("name", food.getName());
		values.put("kc", food.getKc());
		values.put("desc", food.getDesc());
		values.put("typeCode", food.getTypeCode());
		long num = db.insert("food", null, values);
		if(num!=0){
			bool = true;
		}
		return bool;
	}
	@Override
	public boolean saveLocalFoodType(FoodType foodType) {
		boolean bool = false;
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues  values = new ContentValues();
		values.put("foodTypeCode", foodType.getFoodTypeCode());
		values.put("foodTypeName", foodType.getFoodTypeName());
		values.put("parentID", foodType.getParentID());
		long num = db.insert("foodtype", null, values);
		if(num!=0){
			bool = true;
		}
		return bool;
	}
	@Override
	public boolean removeLocalFoodGuide(int userId) {
		boolean bool = false;
		SQLiteDatabase db = helper.getWritableDatabase();
		int num = db.delete("foodguide", "userID=?", new String []{userId+""});
		if(num!=0){
			bool = true;
		}
		db.close();
		return bool;
	}
	@Override
	public void removeLocalFood() {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.delete("food", null, null);
		db.close();
	}
	@Override
	public void removeLocalFoodType() {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.delete("foodtype", null, null);
		db.close();
	}
	@Override
	public BaseModel queryDataByUserId(int userId) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public BaseModel queryDataById(int pk) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public boolean addData(BaseModel model) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean modifData(BaseModel model) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean modifData(ArrayList<BaseModel> datas) {
		// TODO Auto-generated method stub
		return false;
	}
}
